/** 
 * File-Name:ExceptionStackTraceCrawler.java
 *
 * Created on 2011-4-20 下午08:50:36
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package com.exceptionhandler.service.crawl;

import java.util.ArrayList;
import java.util.List;

import org.neocrawler.entity.WebContent;

import com.exceptionhandler.entity.OpenSourceProject;

import data.processor.URLFilter;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-4-20 下午08:50:36
 */
public class HibernateCrawler extends ExceptionCrawler implements
		IExceptionCrawler {
	// ========== Required!!!! ========== //
	private static final String PROJECT_NAME = "Hibernate";
	private static final String PARSER_CLASS_NAME = HibernateCrawler.class
			.getName();
	private static String SEED_URLS = "https://forum.hibernate.org/viewforum.php?f=1&sid=94534cd1dcdc74f6e5ebbe714d9a46fb"
			+ OpenSourceProject.SPLITER
			+ "https://forum.hibernate.org/viewforum.php?f=9&sid=94534cd1dcdc74f6e5ebbe714d9a46fb"
			+ OpenSourceProject.SPLITER
			+ "https://forum.hibernate.org/viewforum.php?f=6&sid=94534cd1dcdc74f6e5ebbe714d9a46fb"
			+ OpenSourceProject.SPLITER
			+ "https://forum.hibernate.org/viewforum.php?f=31&sid=94534cd1dcdc74f6e5ebbe714d9a46fb";

	// ========== Optional ========== //
	private static final String STORE_PATH = BASE_STORE_PATH + PROJECT_NAME
			+ "\\";
	private static final boolean USE_PROXY_OR_NOT = true;
	private static final int THREAD_NUM = 6;
	private static final String PROXY_URL = "192.168.4.31";
	private static final int PROXY_PORT = 3128;

	public HibernateCrawler() {
		super(PROJECT_NAME, SEED_URLS, STORE_PATH, PARSER_CLASS_NAME,
				THREAD_NUM, USE_PROXY_OR_NOT, PROXY_URL, PROXY_PORT);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.neocrawler.parser.IParser#getInitialSeeds()
	 */
	@Override
	public List<WebContent> getInitialSeeds() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.neocrawler.parser.IParser#parse(org.neocrawler.entity.WebContent,
	 * java.lang.String)
	 */
	@Override
	public List<WebContent> parse(WebContent webContent, String webPage) {
		List<WebContent> webContents = new ArrayList<WebContent>();
		String type = webContent.getType().toString();

		if (WebContent.TYPE_SEED_PAGE.equals(type)) {
			// parse it!
			try {
				List<String> urls = URLFilter.getGoodURLList(webPage);
				for (int i = 0; urls != null && i < urls.size(); i++) {
					String url = urls.get(i);
					if (url.contains("viewforum.php")) {
						WebContent w = new WebContent();
						w.setUrl("https://forum.hibernate.org/"
								+ url.substring(url.indexOf("./") + 2).replace(
										"amp;", ""));
						w.setType(WebContent.TYPE_SEED_PAGE);
						w.setParentUrl(webContent.getUrl());
						w.setParentUuid(webContent.getUuid());
						w.setReserve02(PROJECT_NAME);
						webContents.add(w);
					} else if (url.contains("viewtopic.php")) {
						WebContent w = new WebContent();
						w.setUrl("https://forum.hibernate.org/"
								+ url.substring(url.indexOf("./") + 2).replace(
										"amp;", ""));
						w.setType(TYPE_FORUM_THREAD);
						w.setParentUrl(webContent.getUrl());
						w.setParentUuid(webContent.getUuid());
						w.setReserve02(PROJECT_NAME);
						webContents.add(w);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return webContents;
		} else if (TYPE_FORUM_THREAD.equals(type)) {
			// if forum thread, download this page.
			return null;
		} else {
			return null;
		}
	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		HibernateCrawler crawler = new HibernateCrawler();
		// crawler.start();
		crawler.restart();
	}

}
